namespace Brjnk.GpxExplorer.ViewModel
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using Brjnk.Common;
    using Brjnk.CoordinateSystem2D.Layers;

    /// <summary>
    /// </summary>
    public class PhotoRef : IPhoto
    {
        private LocatedPhoto locatedPhoto;

        private Photo photo;

        private WeakEventListener listener;

        public PhotoRef(LocatedPhoto locatedPhoto, Point position)
        {
            AddHandlers(locatedPhoto);
            this.locatedPhoto = locatedPhoto;
            this.position = position;
        }

        public PhotoRef(Photo photo, Point position)
        {
            AddHandlers(photo);
            this.photo = photo;
            this.position = position;
        }

        private void AddHandlers(INotifyPropertyChanged locatedPhoto)
        {
            listener = new WeakEventListener(SourcePropertyChanged);
            PropertyChangedEventManager.AddListener(locatedPhoto, listener, "Thumbnail");
            PropertyChangedEventManager.AddListener(locatedPhoto, listener, "PhotoFull");
            PropertyChangedEventManager.AddListener(locatedPhoto, listener, "Orientation");
            PropertyChangedEventManager.AddListener(locatedPhoto, listener, "IsVisible");
        }

        private bool SourcePropertyChanged(Type managerType, object sender, EventArgs e)
        {
            var args = e as PropertyChangedEventArgs;
            OnPropertyChanged(args.PropertyName);
            return true;
        }

        public System.Windows.Media.ImageSource Thumbnail
        {
            get
            {
                if (locatedPhoto != null)
                    return locatedPhoto.Photo.Thumbnail;
                else
                    return photo.Thumbnail;
            }
        }

        public System.Windows.Media.ImageSource PhotoFull
        {
            get
            {
                if (locatedPhoto != null)
                    return locatedPhoto.Photo.PhotoFull;
                else
                    return photo.PhotoFull;
            }
        }

        private Point position;

        public System.Windows.Point Position
        {
            get { return position; }
        }

        public PhotoOrientation Orientation
        {
            get
            {
                if (locatedPhoto != null)
                    return locatedPhoto.Photo.Orientation;
                else
                    return photo.Orientation;
            }
        }

        public bool IsVisible
        {
            get
            {
                if (locatedPhoto != null)
                    return locatedPhoto.Photo.IsVisible;
                else
                    return photo.IsVisible;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}